#include <stdio.h>  
#include <stdlib.h>
#include <assert.h>

//Structure containing a Data part & a  
//Link part to the next node in the List  
struct Node  
{  
	int Data;  
	struct Node *Next;  
}*Head;  
/* --- Extra Functionality ---*/
int memcount=0;
void *my_malloc (size_t s)
{
	void *s1;
	s1 = malloc(s);
	assert(s1!=NULL);
	memcount++;
	return s1;
}

void my_free(void *ptr){
	free(ptr);  
	memcount--;
}

//Poping from a Stack  
 int popStack()  
{  
	struct Node *cur_ptr=Head;  
	assert(cur_ptr!=NULL);
	int toreturn = Head->Data;
	Head = Head->Next;  
	my_free(cur_ptr);
	return toreturn;  

}     
//Displaying Stack  
void displayStack()  
{  
	struct Node *cur_ptr=Head;  
	assert(cur_ptr!=NULL);  
	printf("\nElements in Stack:\n");  
	while(cur_ptr)  
	{  
	    printf("\t%d\n",cur_ptr->Data);  
	    cur_ptr=cur_ptr->Next;  
	}  
	printf("\n");  
}  
//Pushing into Stack  
void pushIntoStack(int num)  
{  
	struct Node *tmp_ptr=NULL;  
	tmp_ptr=(struct Node *)my_malloc(sizeof(struct Node));
	tmp_ptr->Data=num;  

	if(Head)  
	{  
		tmp_ptr->Next=Head;  
		Head=tmp_ptr;  
	}  
	else { //List is Empty  
		Head=tmp_ptr;  
		Head->Next=NULL;  
	}  
} 
 
int getSize(){
	struct Node *cur_ptr=Head;  
	int size=1;
	if(!Head){
		return 0;
	}
	while(cur_ptr->Next!=NULL){
		cur_ptr=cur_ptr->Next;
		size++;	
	}
	return size;
}
int main(){
	int i;
	for(i=0; i<100; ++i){
		pushIntoStack(i);
		#ifdef __VERIFY
		assert(getSize()==i+1);	
		#endif
	}
	displayStack();
	for(i=0; i<100; ++i){
		#ifdef __VERIFY
		assert(getSize()==100-i);
		#endif
		assert(popStack()==100-i-1);
	}
	assert(memcount==0);
}

